package cn.itcast.bos.service.base.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixedAreaRepository;
import cn.itcast.bos.dao.base.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.CourierService;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.service.base.TakeTimeService;
@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {
	@Autowired
	private FixedAreaRepository fixedAreaRepository;
	@Override
	public void addFixedArea(FixedArea fixedArea) {
		// TODO Auto-generated method stub
		fixedAreaRepository.save(fixedArea);
	}
	
	@Override
	public Page<FixedArea> findAllFixedArea(Specification<FixedArea> sf, Pageable p) {
		// TODO Auto-generated method stub
		Page<FixedArea> findAll = fixedAreaRepository.findAll(sf,p);
		return findAll;
	}
	@Autowired
	private CourierRepository cs;
	@Autowired
	private TakeTimeRepository tt;
	@Override
	public void associationCourierToFixedArea(FixedArea fixedArea, String courierId, String takeTimeId) {
		// TODO Auto-generated method stub
		FixedArea f = fixedAreaRepository.findOne(fixedArea.getId());
		Courier c = cs.findOne(Integer.parseInt(courierId));
		TakeTime t = tt.findOne(Integer.parseInt(takeTimeId));
		f.getCouriers().add(c);
		c.setTakeTime(t);
	}

}
